﻿using System;
using System.Drawing;
using System.Windows.Forms;
using MediaController.Client.DAL;
using System.Diagnostics;
using System.IO;

namespace MediaController.Client.Controls.MainControl
{
    public partial class Main : UserControl
    {
        object _syncObject = new object();
        public Main()
        {
            InitializeComponent();
            Logger.NewLogMessage += Logger_NewLogMessage;
        }

        void Logger_NewLogMessage(string obj)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<string>(LogMessage), obj);
            }
            else
            {
                LogMessage(obj);
            }

        }

        private void LogMessage(string obj)
        {
            lock (_syncObject)
            {
                _listBoxLog.Items.Add(obj);
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            _mediaControllerDataSet = DataAccess.Instance.MediaControllerDataSet;
            _bindingSource.DataSource = _mediaControllerDataSet;
            _comboBoxFilterDate.SelectedIndex = 0;
        }

        private void ComboBoxFilterSelectedIndexChanged(object sender, EventArgs e)
        {
            string filter = string.Format("{0} AND {1}", GetStatusFilter(), GetDateFilter());
            filter = filter.Trim(' ');

            if (filter == "AND") filter = string.Empty;

            if (filter.StartsWith("AND"))
                filter = filter.Substring(4, filter.Length - 4);
            if (filter.EndsWith("AND"))
                filter = filter.Substring(0, filter.Length - 4);

            filter = filter.Trim(' ');

            filesBindingSource.Filter = filter;
        }

        private string GetStatusFilter()
        {
            if (_comboBoxFilterStatus.SelectedValue == null) return string.Empty;
            return (int)_comboBoxFilterStatus.SelectedValue == -1 ? ""
                               : string.Format("StatusId = {0}", (int)_comboBoxFilterStatus.SelectedValue);
        }

        private string GetDateFilter()
        {
            switch (_comboBoxFilterDate.Text)
            {
                case "All":
                    return string.Empty;
                case "Today":
                    var todayDateStart = DateTime.Today;
                    return string.Format("StartTime >= '{0}' AND EndTime < '{1}'", todayDateStart, todayDateStart.AddDays(1));
                case "This Week":
                    var d = (int)DateTime.Now.DayOfWeek;
                    var weekDateStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day - d);
                    return string.Format("StartTime >= '{0}' AND EndTime < '{1}'", weekDateStart, weekDateStart.AddDays(7));
                case "This Month":
                    var monthDateStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                    return string.Format("StartTime >= '{0}' AND EndTime < '{1}'", monthDateStart, monthDateStart.AddMonths(1));
            }
            return string.Empty;
        }

        private void CToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (_dataGridView.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow selectedRow in _dataGridView.SelectedRows)
                {
                    string fileName = selectedRow.Cells["fullPathDataGridViewTextBoxColumn"].Value.ToString();
                    string directory = Path.GetDirectoryName(fileName);
                    Process.Start(directory);
                }
            }
        }
    }

}
